from datetime import datetime

import scrapy

from xiaodao.items import LatestPostItem

current_time = datetime.now()


class XldaoSpider(scrapy.Spider):
    name = "xldao"
    allowed_domains = ["www.x6g.com"]
    start_urls = ["https://www.x6g.com"]

    def parse(self, response):
        elements = response.xpath('//li[@class="new"]/a[@target="_blank"]')

        for element in elements:
            text = element.xpath('./text()').extract_first()
            url = element.xpath('./@href').extract_first()

            if text and url:
                item = LatestPostItem()
                item["text"] = text
                item["url"] = url
                item["time"] = current_time
                yield item
